package com.H1023.controller;

import com.H1023.dao.LoginMapper;
import com.H1023.pojo.Admin;
import com.H1023.pojo.Reader;
import com.H1023.service.LoginService;
import com.H1023.service.LoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class LoginController {

    @Autowired
    @Qualifier("loginServiceImpl")
    private LoginService loginService;


    @RequestMapping("/user/login")
    public String login( String name,  String pwd){
        System.out.println("login");
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", name);
        map.put("pwd", pwd);
        Reader reader = loginService.login(map);

        if(reader!=null){
            return Integer.toString(reader.getReaderId());
        }else{
            return "-1";
        }


    }


    @RequestMapping("/user/register")
    public String register( String name, String pwd,  String sex){
        System.out.println("register");
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", name);
        map.put("pwd", pwd);
        map.put("sex", sex);
        int res = loginService.register(map);
        if(res==1){
            return "1";
        }else{
            return "-1";
        }

    }



    @RequestMapping("/admin/login")
    public String adminLogin( String name,  String pwd){
        System.out.println("adminLogin");
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", name);
        map.put("pwd", pwd);
        Admin admin = loginService.adminLogin(map);

        if(admin!=null){
            return Integer.toString(admin.getUserid());
        }else{
            return "-1";
        }
    }

    @RequestMapping("/admin/register")
    public String register( String name,  String pwd){
        System.out.println("register");
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", name);
        map.put("pwd", pwd);
        int res = loginService.adminRegister(map);
        if(res==1){
            return "1";
        }else{
            return "-1";
        }

    }

}

